package com.boboPlanet.user.service.impl;

import com.boboPlanet.comm.context.BusinessStartTimeContext;
import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.utils.AutoGenerateIds;
import com.boboPlanet.user.async.AsyncTask;
import com.boboPlanet.user.entity.dto.ArtTypeDTO;
import com.boboPlanet.user.entity.dto.SysLogDTO;
import com.boboPlanet.user.entity.po.ArtTypePO;
import com.boboPlanet.user.entity.vo.ArtTypePageVO;
import com.boboPlanet.user.entity.vo.ArtTypeVO;
import com.boboPlanet.user.enums.SysOperatorEnum;
import com.boboPlanet.user.mapper.ArticleTypeMapper;
import com.boboPlanet.user.mapper.UserInfoConvert;
import com.boboPlanet.user.service.ArticleTypeService;
import com.boboPlanet.user.single.SingleFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/2/23
 **/
@Service
@Slf4j
public class ArticleTypeServiceImpl implements ArticleTypeService {

    @Resource
    private ArticleTypeMapper articleTypeMapper;

    @Resource
    private AsyncTask asyncTask;

    @Override
    public List<ArtTypeVO> getAllArtTypes() {
        List<ArtTypePO> pos = articleTypeMapper.getAllArtTypes();
        return UserInfoConvert.INSTANCE.typePos2Vos(pos);
    }

    @Override
    public Boolean addArtTypes(String typeName) {
        if (!Optional.ofNullable(typeName).isPresent()){
            throw BusinessException.build("类型名称不能为空...");
        }
        if (StringUtils.isBlank(typeName)){
            throw BusinessException.build("类型名称不能为空...");
        }
        typeName = typeName.trim();
        boolean exist = articleTypeMapper.judgeTypeNameIsExist(typeName) > 0;
        if (exist){
            throw BusinessException.build("该分类已存在,不能重复,请重新添加...");
        }
        ArtTypePO po = new ArtTypePO();
        po.setTypeId(AutoGenerateIds.getGenerateId());
        po.setTypeName(typeName);
        po.setInvalidFlag(0);
        po.setPlanetUpdateTime(BusinessStartTimeContext.getTime());
        po.setPlanetUpdateUser(LoginUserContext.getLoginUserInfoPO().getUserName());
        po.setVersion(0);

        SysLogDTO logDto = SingleFactory.getLogDto(SysOperatorEnum.INSERT_ART_TYPE.getDesc());
        asyncTask.tranceLog(logDto);
        return articleTypeMapper.insert(po) > 0;
    }

    @Override
    public Boolean updateArtTypes(String typeName,Long typeId) {
        boolean exist = articleTypeMapper.judgeTypeNameIsExist(typeName) > 0;
        if (exist){
            throw BusinessException.build("该分类已存在,不能重复,请重新添加...");
        }
        ArtTypePO po = new ArtTypePO();
        po.setTypeId(typeId);
        po.setTypeName(typeName);
        po.setPlanetUpdateTime(BusinessStartTimeContext.getTime());
        po.setPlanetUpdateUser(LoginUserContext.getLoginUserInfoPO().getUserName());

        SysLogDTO logDto = SingleFactory.getLogDto(SysOperatorEnum.UPDATE_ART_TYPE.getDesc());
        asyncTask.tranceLog(logDto);

        return articleTypeMapper.updateById(po) > 0;
    }

    @Override
    public ArtTypePageVO getAllArtTypesByPage(ArtTypeDTO dto) {
        dto.setPage(dto.getPage() - 1);
        List<ArtTypeVO> vos = articleTypeMapper.getAllArtTypesByPage(dto);
        if (vos.isEmpty()){
            return new ArtTypePageVO(0, Collections.emptyList());
        }
        ArtTypePageVO vo = new ArtTypePageVO();
        vo.setTotals(vos.get(0).getTotals());
        vo.setArtVos(vos);

        return vo;
    }

    @Override
    public Boolean deleteByIds(List<Long> typeIds) {
        String updater = LoginUserContext.getLoginUserInfoPO().getUserName();
        return articleTypeMapper.cancelType(typeIds,updater);
    }

    @Override
    public Boolean resumeTypeByIds(List<Long> typeIds) {
        String updater = LoginUserContext.getLoginUserInfoPO().getUserName();
        return articleTypeMapper.resumeTypeByIds(typeIds,updater);
    }
}
